﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;

namespace WFIServiceBehaviorExtension
{
    /// <summary>
    /// 用于实现客户端在调用WCF服务时自动附加用户信息（用户名及密码）
    /// </summary>
    public class AttachUserNamePasswordInspector : IClientMessageInspector
    {
        private static string userName = System.Configuration.ConfigurationManager.AppSettings["username"];
        private static string password = System.Configuration.ConfigurationManager.AppSettings["pwd"];
        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            throw new NotImplementedException();
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            MessageHeader userNameHeader = MessageHeader.CreateHeader("OperationUserName", "http://www.wfi.cn", userName, false, "");
            MessageHeader pwdNameHeader = MessageHeader.CreateHeader("OperationPwd", "http://www.wfi.cn", password, false, "");

            request.Headers.Add(userNameHeader);
            request.Headers.Add(pwdNameHeader);
            return null;
        }
    }
}
